package com.zzl.study.auth.security.authentication;

import lombok.Data;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

/**
 * @author: zhangzl
 * @date: 2024/3/12 21:10
 * @version: 1.0
 * @description: 自定义Web端认证详情
 */
@Data
public class CustomWebAuthenticationDetails implements Transfer<WebAuthenticationDetails> {

    /**
     * 请求IP地址
     */
    private String remoteAddress;

    /**
     * session id
     */
    private String sessionId;

    @Override
    public WebAuthenticationDetails transform() {
        return new WebAuthenticationDetails(this.remoteAddress, this.sessionId);
    }

    /**
     * 将Security框架中的Web端认证详情转换为自定义Web端认证详情
     *
     * @param webAuthenticationDetails Security框架中的Web端认证详情
     * @return 自定义Web端认证详情
     */
    public static CustomWebAuthenticationDetails reTransform(WebAuthenticationDetails webAuthenticationDetails) {
        if (webAuthenticationDetails == null) {
            return null;
        }
        CustomWebAuthenticationDetails customWebAuthenticationDetails = new CustomWebAuthenticationDetails();
        customWebAuthenticationDetails.setRemoteAddress(webAuthenticationDetails.getRemoteAddress());
        customWebAuthenticationDetails.setSessionId(webAuthenticationDetails.getSessionId());
        return customWebAuthenticationDetails;
    }

}
